#include <Windows.h>
#include <tchar.h>
#include <gdiplus.h>

#include "Core.h"
#include "UI.h"

//Enable Visual Styles
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' \
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#pragma comment( lib, "CassiopeiaLibrary.lib" )
#pragma comment( lib, "Wtsapi32.lib" )
#pragma comment( lib, "gdiplus.lib" )
#pragma comment( lib, "winmm.lib" )
//#pragma comment( lib, "vfw32.lib" )

//ToDo:
//add unauth-special functions
//add lid functions

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
	//allow only one instance of Aurora
	HANDLE hMutex = CreateMutex(0, FALSE, TEXT("{07253B7F-69B7-4A06-A99E-771D23CF56D7}"));
	if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
	{
		ReleaseMutex(hMutex);

		CloseHandle(hMutex);
		hMutex = 0;

		return 0;
	}
	ReleaseMutex(hMutex);


	//initialize gdi+
	ULONG_PTR gdiplusToken;  
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);


	//manipulate working directory, so that it is always the same directory as the executable
	TCHAR path[_MAX_PATH] = {0};
	TCHAR drive[_MAX_DRIVE] = {0};
	TCHAR dir[_MAX_DIR] = {0};

	GetModuleFileName(0, path, _MAX_PATH);

	_tsplitpath(path, drive, dir, 0, 0);
	_tmakepath(path, drive, dir, 0, 0);

	SetCurrentDirectory(path);

	//start Aurora
	UI ui(hInstance);

	MSG msg;
	while(GetMessage(&msg,0,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	//shutdown gdi+
	GdiplusShutdown(gdiplusToken);
	
	return 0;
}
